import { ProjectName } from '@/config/constants'
import { Notification } from '@arco-design/web-vue'

export const isDev = process.env.NODE_ENV === 'development'

export function setPageTitle(titleText: string | unknown) {
  window.document.title = `${titleText ? ` ${titleText} - ` : ''}${ProjectName}`
}

// 新标签预览
export function documentView({ style = '', content }: any) {
  const newWindow = window.open('', '_blank')
  newWindow?.document.write(`<style>${style}</style><div style="width: 80%; margin: 0 auto;">${content}</div>`)
}

export const uuid = (prefix = 'app-') => {
  const s: any = []
  const hexDigits = '0123456789abcdef'
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.slice(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.slice((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'

  const uuid = s.join('')
  return prefix + uuid
}

// 下载二维码
export const downLoadQRcode = (id: string, title = '二维码'): void => {
  const canvas = document.getElementById(id) as HTMLCanvasElement
  const url = canvas.toDataURL('image/png') // 通过 toDataURL 返回一个包含图片展示的 data URI
  const aDom = document.createElement('a')
  aDom.download = title // 设置下载的文件名
  aDom.href = url
  document.body.appendChild(aDom)
  aDom.click()
  aDom.remove()
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data: any, id?: string, parentId?: string, children?: any) {
  const config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children',
  }

  const childrenListMap: any = {}
  const nodeIds: any = {}
  const tree: any = []

  if (!data) {
    return
  }

  for (const d of data) {
    const parentId = d[config.parentId]
    if (!childrenListMap[parentId]) {
      childrenListMap[parentId] = []
    }
    nodeIds[d[config.id]] = d
    childrenListMap[parentId].push(d)
  }

  for (const d of data) {
    const parentId = d[config.parentId]
    if (!nodeIds[parentId]) {
      tree.push(d)
    }
  }

  for (const t of tree) {
    adaptToChildrenList(t)
  }

  function adaptToChildrenList(o: any) {
    if (childrenListMap[o[config.id]]) {
      o[config.childrenList] = childrenListMap[o[config.id]]
    }
    if (o[config.childrenList]) {
      for (const c of o[config.childrenList]) {
        adaptToChildrenList(c)
      }
    }
  }

  return tree
}

/**
 * Base64 编码
 * @param str
 * @returns {string|void}
 */
export function btoa(str: string) {
  return str ? window.btoa(unescape(encodeURIComponent(JSON.stringify(str)))) : console.warn('str不能为空')
}

/**
 * Base64 解码
 * @param str
 * @returns {string|void}
 */
export function atob(str: string) {
  return str ? decodeURIComponent(escape(window.atob(str))) : console.warn('str不能为空')
}

// 输出通知
export const notificationInfo = (content: string) => {
  Notification.info({
    title: '友情提醒',
    content,
  })
}

// 输出错误
export const notificationError = (content: string) => {
  Notification.error({
    title: '友情提醒',
    content,
  })
}

// 输出警告
export const notificationWarning = (content: string) => {
  Notification.warning({
    title: '友情提醒',
    content,
  })
}

// 输出成功
export const notificationSuccess = (content: string) => {
  Notification.success({
    title: '友情提醒',
    content,
  })
}
